﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using MSSQL.DBSync.UI.Common.AsyncCmds;
using MSSQL.DBSync.UI.Common.Messages;
using MSSQL.DBSync.UI.Components.MakeComparsion;
using MSSQL.DBSync.UI.Components.MakeSnapshot;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using MSSQL.DBSync.UI.Components.MainChoice;
using MSSQL.DBSync.UI.Components.ManageConn;

namespace MSSQL.DBSync.UI.Components.MainWindow
{

    /// <summary>
    /// 主窗体视图模型
    /// </summary>
    public class MainWindowVM : ViewModelBase, IMainWindowVM
    {
        public MainWindowVM()
        {
            if (IsInDesignMode)
            {
                initDesignData();
            }
            else
            {
                initDesignData();
            }
        }

        /// <summary>
        /// 初始化设计数据
        /// </summary>
        private void initDesignData()
        {
            Title = "SQL Server数据库结构升级工具";

            Menus.Add(new SideMenuVO { Name = "选择功能", ContentKey = typeof(IMainChoiceView) });
            Menus.Add(new SideMenuVO { Name = "数据结构快照", ContentKey = typeof(IMakeSnapshotView) });
            Menus.Add(new SideMenuVO { Name = "数据库升级", ContentKey = typeof(IMakeComparsionView) });
            Menus.Add(new SideMenuVO { Name = "连接管理", ContentKey = typeof(IManageConnView) });


            ////注册消息中心
            //Messenger.Default.Register<Type>(this, MessageTopics.APP_GotoPage, GotoPageHandler);

            Messenger.Default.Register<Type>(this, MessageTopics.APP_Title, PageTitleHandler);

            //CurMenuItem = Menus[0]; //默认选择第一条
        }

        private void PageTitleHandler(Type pagettype)
        {
            var mitem = Menus.FirstOrDefault(x => x.ContentKey == pagettype);
            if (mitem != null)
            {
                Title = mitem.Name;
            }
        }


        private string mTitle;
        /// <summary>
        /// 主页标题
        /// </summary>
        public string Title
        {
            get => mTitle;
            set => Set(ref mTitle, value);
        }



        private AsyncCommand mSetFirstContent;
        /// <summary>
        /// 初始化设置首页内容
        /// </summary>
        public AsyncCommand SetFirstContent
        {
            get
            {

                //非空判断
                if (mSetFirstContent != null) { return mSetFirstContent; }

                mSetFirstContent = new AsyncCommand(async () =>
                    {
                        if (!IsInDesignMode && Menus.Any())
                        {
                            //MessageBox.Show(mCurMenuItem.Name);
                            //View.NaviToConten(Menus[0].ContentKey);
                            //Title = Menus[0].Name;
                            //Messenger.Default.Send<Type>(CurMenuItem.ContentKey, MessageTopics.APP_GotoPage);

                            CurMenuItem = Menus[0];
                        }
                    }
                );

                return mSetFirstContent;
            }

            set { mSetFirstContent = value; }
        }

        public ObservableCollection<SideMenuVO> Menus { get; set; } = new ObservableCollection<SideMenuVO>();

        private SideMenuVO mCurMenuItem;
        /// <summary>
        /// 当前菜单
        /// </summary>
        public SideMenuVO CurMenuItem
        {
            get => mCurMenuItem;
            set
            {
                mCurMenuItem = value;
                if (mCurMenuItem !=  null )
                {
                    //设置内容页 
                    //MessageBox.Show(mCurMenuItem.Name);
                    //View.NaviToConten(mCurMenuItem.ContentKey);
                    //Title = mCurMenuItem.Name;
                    Messenger.Default.Send<Type>(CurMenuItem.ContentKey,MessageTopics.APP_GotoPage);
                }
            }
        }


    }
}
